package com.blade.impl;

import com.blade.serivce.KindeditorService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Kindeditor 富文本编辑器
 * @author 17030357
 */
@Service
public class KindeditorServiceImpl implements KindeditorService {
    private String error = "error";
    private Integer errorValue = 1;
    private String message = "message";

    /**
     * 图片地址
     */
    private String imgHost = null;

    /**
     * 图片下载url
     */
    private String imgUploadUrl = null;

    /**
     * 图片服务器验证用户名
     */
    private String imgUserName = null;

    /**
     * 图片类型
     */
    private String imgImageType = null;

    /**
     * 图片服务器验证密码
     */
    private String imgPassword = null;

    /**
     * 保存图片，返回存储图片信息
     */
    @Override
    public Map<String, Object> saveImg(MultipartFile img, String imageSize) throws IOException {

        Map<String, Object> relInfo = new HashMap<String, Object>();

        InputStream is = null;
        BufferedImage sourceImg = null;

        try {
            is = img.getInputStream();
            sourceImg = ImageIO.read(is);

            //重置读取位置
            is = img.getInputStream();

            if (sourceImg == null) {
                relInfo.put(error, errorValue);
                relInfo.put(message, "您上传的不是图片类型文件");

                return relInfo;
            }
        } catch (IOException e) {
            relInfo.put(error, errorValue);
            relInfo.put(message, "上传图片异常，保存失败");

            try {
                if (null != is)
                {
                    is.close();
                }
            } catch (IOException ioException) {
                throw ioException;
            }

            return relInfo;
        }

        String size = sourceImg.getWidth() + "*" + sourceImg.getHeight();

        if (imageSize != null && !size.equals(imageSize)) {
            relInfo.put(error, errorValue);
            relInfo.put(message, "图片规格必须是：" + imageSize);
            return relInfo;
        }

        //上传文件
        String imgRequestPath = uploadEditorImgToPicServer(is);

        if (imgRequestPath == null) {
            relInfo.put(error, errorValue);
            relInfo.put(message, "上传图片异常，保存失败");
        } else {
            relInfo.put(error, 0);
            relInfo.put("url", imgRequestPath);
        }

        return relInfo;
    }

    /**
     * 保存编辑器上传的单个图片到图片服务器
     * @param in 图片文件
     * @return 图片对应图片服务器的访问地址
     */
    private String uploadEditorImgToPicServer(InputStream in) {
        String imgRequestPath = null;

        Properties prop = new Properties();
        prop.put("IMG_SERVER_URL", imgUploadUrl);

        String appcode = imgUserName;
        String imageType = imgImageType;
        String password = imgPassword;

        return imgRequestPath;
    }

    public void setImgHost(String imgHost) {
        this.imgHost = imgHost;
    }

    public void setImgUploadUrl(String imgUploadUrl) {
        this.imgUploadUrl = imgUploadUrl;
    }

    public void setImgUserName(String imgUserName) {
        this.imgUserName = imgUserName;
    }

    public void setImgImageType(String imgImageType) {
        this.imgImageType = imgImageType;
    }

    public void setImgPassword(String imgPassword) {
        this.imgPassword = imgPassword;
    }
}